package cn.hotel.controller.user;

import cn.hotel.entity.Order;
import cn.hotel.response.ResponseTool;
import cn.hotel.response.Result;
import cn.hotel.service.OrderService;
import cn.hotel.service.RoomTypeService;
import cn.hotel.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 订单接口
 */
@Slf4j
@RestController
@RequestMapping(value = "/user/order")
public class UserOrderController {
    @Resource
    private UserService userService;

    @Resource
    private OrderService orderService;

    @Resource
    private RoomTypeService roomTypeService;

    /**
     * 根据用户Id,获取该用户的全部订单信息
     * @param userId
     * @return
     */
    @PostMapping
    public Result getOrdersByUserId(String userId){
        log.info("userid===={}",userId);
        if (userId==null){
            return ResponseTool.failed("查询失败,请输入用户id");
        }
        List<Order> ordersList = orderService.getOrdersByUserId(userId);

        for (Order order : ordersList) {
            System.out.println(order);
        }
        return ResponseTool.success(ordersList);
    }

    /**
     * 预定房间，
     * @param order
     * @return
     */
    @PostMapping("/add")
    public Result saveOrder(Order order){
        log.info("order{}",order);
        int result = orderService.addOrder(order);
        //预定成功后，对类型的房间数减一
        return result==1?ResponseTool.success("预定成功,请前往支付"):ResponseTool.failed("预定失败");
    }

    /**
     * 查询指定的订单信息
     * @param orderId
     * @return
     */
    @GetMapping("/{orderId}")
    public Result getOrderById(@PathVariable("orderId") Integer orderId){
        log.info("orderId{}",orderId);
        Order orderById = orderService.getOrderById(orderId);
        return ResponseTool.success(orderById);
    }

    /**
     * 订单支付，
     * @param orderId
     * @param password
     * @param  userId
     * @return
     */
    @PostMapping("/pay")
    public Result payOrder(String orderId, String password, String userId){
        log.info("orderId=={},password=={},userId=={}",orderId,password,userId);
        if (orderService.payOrder(userId, password, orderId)==1){
            return ResponseTool.success("支付成功");
        }

        return ResponseTool.failed("支付失败请检查密码");
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @PostMapping("/cancel")
    public Result cancelOrder(Integer orderId){
        log.info("orderId={}",orderId);
        int result = orderService.cancelOrder(orderId);

        if (result==0){
            return ResponseTool.failed("取消失败");
        }
        return ResponseTool.success(1);
    }
}
